﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

namespace SGArchiveSystem7x.Models
{
    public class CDepartmentAuthorityModel
    {
        #region 属性
        /// <summary>
        /// 系统ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 部门ID
        /// </summary>
        public int iDepartmentID { get; set; }
        /// <summary>
        /// 部门名称
        /// </summary>
        public string sDepartmentName { get; set; }
        /// <summary>
        /// 是否可以浏览
        /// </summary>
        public int iShow { get; set; }
        /// <summary>
        /// 是否可以看电子件
        /// </summary>
        public int iView { get; set; }
        /// <summary>
        /// 是否可以编辑
        /// </summary>
        public int iEdit { get; set; }
        /// <summary>
        /// 是否可以审批
        /// </summary>
        public int iApproval { get; set; }
        /// <summary>
        /// 是否可以下载
        /// </summary>
        public int iDown { get; set; }
        /// <summary>
        /// 是否可以打印
        /// </summary>
        public int iPrint { get; set; }
        #endregion

        /// <summary>
        /// 建立部门权限
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="listDepartment"></param>
        /// <returns></returns>
        public static List<CDepartmentAuthorityModel> CreateModel(int departmentId, List<CDepartmentTreeModel> listDepartment)
        {
            var list = new List<CDepartmentAuthorityModel>();

            var listAuth = T_DepartmentAuthorityDal.Get_DepartmentAuthority(departmentId);
            
            if(listAuth == null || listAuth.Count == 0)
            {
                #region 如果没有部门权限，则初始化部门权限
                foreach (var n in listDepartment)
                {
                    CDepartmentAuthorityModel en = new CDepartmentAuthorityModel();
                    if(n.childDepartment.Count == 0)
                    {
                        en.iApproval = en.iDown = en.iEdit = en.iPrint = en.iShow = en.iView = 0;
                        if (n.Deparetment.Dep_ID == departmentId)
                            en.iPrint = en.iShow = en.iView = 1;
                        en.sDepartmentName = n.Deparetment.Dep_Name;
                        en.iDepartmentID = n.Deparetment.Dep_ID;
                        en.ID = 0;
                        list.Add(en);
                    }
                    else
                    {
                        ////如果有子部门，那么就设为-1，不处理
                        en.iApproval = en.iDown = en.iEdit = en.iPrint = en.iShow = en.iView = -1;
                        en.sDepartmentName = n.Deparetment.Dep_Name;
                        en.iDepartmentID = n.Deparetment.Dep_ID;
                        en.ID = 0;
                        list.Add(en);

                        foreach(var cn in n.childDepartment)
                        {
                            CDepartmentAuthorityModel en2 = new CDepartmentAuthorityModel();
                            en2.iApproval = en2.iDown = en2.iEdit = en2.iPrint = en2.iShow = en2.iView = 0;
                            if (cn.Dep_ID == departmentId)
                                en2.iPrint = en2.iShow = en2.iView = 1;
                            en2.sDepartmentName = "   " + cn.Dep_Name;
                            en2.iDepartmentID = cn.Dep_ID;
                            en2.ID = 0;
                            list.Add(en2);
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 如果有部门权限，则获取部门权限
                foreach(var n in listDepartment)
                {
                    CDepartmentAuthorityModel en = new CDepartmentAuthorityModel();
                    if(n.childDepartment.Count == 0)
                    {
                        foreach (var an in listAuth)
                        {
                            if(n.Deparetment.Dep_ID == an.DepAut_DtoID)
                            {
                                en.iApproval = an.DepAut_Approval;
                                en.iDepartmentID = an.DepAut_DtoID;
                                en.iDown = an.DepAut_Down;
                                en.iEdit = an.DepAut_Edit;
                                en.iPrint = an.DepAut_Print;
                                en.iShow = an.DepAut_Show;
                                en.iView = an.DepAut_View;
                                en.sDepartmentName = n.Deparetment.Dep_Name;
                                en.ID = an.DepAut_ID;
                                list.Add(en);
                                break;
                            }
                        }
                    }
                    else
                    {
                        ////如果有子部门，那么就设为-1，不处理
                        en.iApproval = en.iDown = en.iEdit = en.iPrint = en.iShow = en.iView = -1;
                        en.sDepartmentName = n.Deparetment.Dep_Name;
                        en.iDepartmentID = n.Deparetment.Dep_ID;
                        en.ID = n.Deparetment.Dep_ID;
                        list.Add(en);

                        foreach (var cn in n.childDepartment)
                        {
                            CDepartmentAuthorityModel en2 = new CDepartmentAuthorityModel();
                            foreach (var an in listAuth)
                            {
                                if (cn.Dep_ID == an.DepAut_DtoID)
                                {
                                    en2.iApproval = an.DepAut_Approval;
                                    en2.iDepartmentID = an.DepAut_DtoID;
                                    en2.iDown = an.DepAut_Down;
                                    en2.iEdit = an.DepAut_Edit;
                                    en2.iPrint = an.DepAut_Print;
                                    en2.iShow = an.DepAut_Show;
                                    en2.iView = an.DepAut_View;
                                    en2.sDepartmentName = cn.Dep_Name;
                                    en2.ID = an.DepAut_ID;
                                    list.Add(en2);
                                    break;
                                }
                            }
                        }
                    }
                    
                }
                #endregion
            }

            return list;
        }


        public static List<CDepartmentAuthorityModel> CreateModelNew(int departmentId, List<CDepartmentTreeModel> listDepartment)
        {
            var en = SGArchiveSystem7xDB.Linq.T_DeparetmentDal.Get_Department(departmentId);
            List<CDepartmentAuthorityModel> list = new List<CDepartmentAuthorityModel>();
            string[] shows = (en.Dep_Show == null ? new string[0] : en.Dep_Show.Split(','));
            string[] edits = (en.Dep_Edit == null ? new string[0] : en.Dep_Edit.Split(','));
            string[] prints = (en.Dep_Print == null ? new string[0] : en.Dep_Print.Split(','));
            string[] downs = (en.Dep_Down == null ? new string[0] : en.Dep_Down.Split(','));
            string[] apps = (en.Dep_Approval == null ? new string[0] : en.Dep_Approval.Split(','));
            string[] views = (en.Dep_View == null ? new string[0] : en.Dep_View.Split(','));
            foreach (var n in listDepartment)
            {
                if(n.childDepartment.Count == 0)
                {
                    CDepartmentAuthorityModel model = new CDepartmentAuthorityModel();
                    model.sDepartmentName = n.Deparetment.Dep_Name;
                    model.iDepartmentID = n.Deparetment.Dep_ID;
                    string id = model.iDepartmentID.ToString();
                    if (shows.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iShow = 1;
                    else
                        model.iShow = 0;

                    if (edits.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iEdit = 1;
                    else
                        model.iEdit = 0;

                    if (prints.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iPrint = 1;
                    else
                        model.iPrint = 0;

                    if (downs.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iDown = 1;
                    else
                        model.iDown = 0;

                    if (apps.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iApproval = 1;
                    else
                        model.iApproval = 0;

                    if (views.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                        model.iView = 1;
                    else
                        model.iView = 0;

                    list.Add(model);
                }
                else
                {
                    //CDepartmentAuthorityModel model = new CDepartmentAuthorityModel();
                    //model.sDepartmentName = n.Deparetment.Dep_Name;
                    //model.iDepartmentID = n.Deparetment.Dep_ID;
                    //model.iApproval = model.iDown = model.iEdit = model.iPrint = model.iShow = model.iView = -1;
                    //list.Add(model);

                    foreach(var nn in n.childDepartment)
                    {
                        CDepartmentAuthorityModel model2 = new CDepartmentAuthorityModel();
                        model2.sDepartmentName = nn.Dep_Name;
                        model2.iDepartmentID = nn.Dep_ID;

                        string id = model2.iDepartmentID.ToString();
                        if (shows.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iShow = 1;
                        else
                            model2.iShow = 0;

                        if (edits.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iEdit = 1;
                        else
                            model2.iEdit = 0;

                        if (prints.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iPrint = 1;
                        else
                            model2.iPrint = 0;

                        if (downs.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iDown = 1;
                        else
                            model2.iDown = 0;

                        if (apps.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iApproval = 1;
                        else
                            model2.iApproval = 0;

                        if (views.Select(c => c).Where(c => c.Contains(id)).Count() > 0)
                            model2.iView = 1;
                        else
                            model2.iView = 0;

                        list.Add(model2);
                    }
                }
            }

            return list;
        }
    }
}